package main

import (
	"github.com/1141520944/go-library/hjz/common"
	"zinxLearn/pkg/zinx/ziface"
	"zinxLearn/pkg/zinx/znet"
)

func main() {
	l := common.LoggerDefault("zinx_server.log", "./tqlogger")
	server := znet.NewZServer("zinx_server", l, znet.WithIP("0.0.0.0"), znet.WithPort("8000"), znet.WithIPVersion("tcp4"))
	server.AddRouter(0, new(ping))
	server.AddRouter(1, new(handler))
	server.Server()
}

type handler struct {
	znet.BaseRouter
}

func (*handler) Handle(req ziface.IRequest) {
	println(string(req.ZData().GetData()))
	if err := req.ZConn().SendMsg(znet.NewMessage(req.ZData().GetMsgID(), []byte("hello ..."))); err != nil {
		println(err.Error())
	}
}

type ping struct {
	znet.BaseRouter
}

func (*ping) Handle(req ziface.IRequest) {
	tmp := string(req.ZData().GetData())
	if tmp != "ping" {
		if err := req.ZConn().SendMsg(znet.NewMessage(req.ZData().GetMsgID(), req.ZData().GetData())); err != nil {
			println(err.Error())
		}
	}
	if err := req.ZConn().SendMsg(znet.NewMessage(req.ZData().GetMsgID(), []byte("pong"))); err != nil {
		println(err.Error())
	}
}
